import 'package:dio/dio.dart';
import 'package:get/get.dart' hide Response;
import 'package:toatool/src/data/model/upgrade.dart';
import 'package:toatool/src/routes/middleWare.dart';
import 'package:toatool/src/widgets/custom_dialog.dart';

import 'platform_utils.dart';

class UpgradeUtil {
  static Future<void> upgarde(Response response) async {
    response.data['data'] = {
      'platform': 1, // 平台名称 0 iOS, 1 Android
      'appVerisonName': '1.0.1', // 版本名称
      'appVerisonCode': 2, // 版本号
      'minVerisonCode': 1, // 需升级的最小版本号
      'contents': [
        // 升级内容
        '优化使用体验，使用更流畅',
        '修复了已知问题',
      ],
      'url': ''
    };
    // 登录拦截
    var upgrade =
        Upgrade.fromJson(response.data['data'] as Map<String, dynamic>);
    var appVerisonCode = await PlatformUtils.getBuildNum();
    var appVerisonCodeNum = int.tryParse(appVerisonCode) ?? 1;
    var _barrierDismissible = true;
    if (appVerisonCodeNum < upgrade.minVerisonCode) {
      _barrierDismissible = false;
    }
    var routing = MiddleWare.routing;
    if (routing != null) {
      if (routing.isBack == false) {
        var customDialogWidget = CustomDialogWidget(
          type: DialogType.upgrade,
          ctx: Get.context!,
          upgrade: upgrade,
          barrierDismissible: _barrierDismissible, //可忽略的障碍
          onConfirmStr: '更新へ',
          onCancelStr: _barrierDismissible ? '閉じる' : null,
          onConfirm: () {
            Get.back<void>(result: true);
            return;
          },
          onCancel: _barrierDismissible
              ? () {
                  Get.back<void>(result: false);
                  return;
                }
              : null,
        );
        var result = customDialogWidget.customShowDialog();
        print(result);
      }
    }
  }
}
